package com.briup.estore.mapper;

import java.math.BigInteger;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.briup.estore.bean.ShopCar;
import com.briup.estore.bean.ShopCarItem;

@Mapper
public interface IShopCarMapper {

	@Select("select id,total,customer_id from es_shopcar where customer_id = #{customer_id} ")
	ShopCar findShopCarByCUstomerId(BigInteger customer_id);

	@Insert("insert into es_shopcar(id,total,customer_id) values(#{id},#{total},#{customer_id}) ")
	@SelectKey(statement = "select my_seq.nextval from dual ", keyProperty = "id", resultType = BigInteger.class, before = true)
	int newShopCar(ShopCar shopCar);

	@Update("update es_shopcar set total=#{total} where id=#{id} ")
	int updateShopCar(ShopCar shopCar);

	@Insert("insert into es_shopcaritem(id,num,cost,book_id,shopcar_id) values(#{id},#{num},#{cost},#{book_id},#{shopcar_id}) ")
	@SelectKey(statement = "select my_seq.nextval from dual ", keyProperty = "id", resultType = BigInteger.class, before = true)
	int insertShopCarItem(ShopCarItem shopCarItem);

	@Select("select a.id car_id,a.total car_total,a.CUSTOMER_ID car_customer_id,"
			+ "b.id item_id,b.num item_num,b.cost item_cost,b.book_id item_book_id,b.shopcar_id  item_shopcar_id ,"
			+ "c.id book_id,c.name book_name, c.price book_price,"
			+ "c.author book_author,c.publisher book_publisher,c.pubdate book_pubdate,"
			+ "c.description book_description,c.category_id book_category_id,"
			+ "c.images book_images,c.click book_click " + "from es_shopcar a,es_shopcaritem b,es_book c "
			+ "where a.id = b.shopcar_id and c.id = b.book_id and a.customer_id = #{customer_id} ")
	@ResultMap("com.briup.estore.mapper.ICategoryMapper.shopCarResultMap")
	ShopCar findShopCarWithShopCarItemWIthBookByCustomerId(BigInteger customer_id);

	@Select("select id,num,cost,book_id,shopcar_id from es_shopcaritem where id = #{id} ")
	ShopCarItem findShopCarItemById(BigInteger id);

	@Delete("delete from es_shopcaritem where id = #{id}")
	int deleteShopCarItem(BigInteger id);

	int deleteShopCarItems(@Param("item_ids") List<BigInteger> item_ids);

	List<ShopCarItem> findShopCarItemByIds(@Param("list") List<BigInteger> select);
}
